package com.demo.rocketmq.sender;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 发送事务消息
 */
@Service
@Slf4j
public class TransactionMqSender {

    @Autowired
    private TransactionMQProducer buildGoodsTransactionProducer;

    public SendResult sendMsg(String topic, String msg, String tags, String keys) throws MQClientException {
        log.info("MQ发送事务消息，topic = {}，tags = {}，keys = {}，msg = {}", topic, tags, keys, msg);
        Message message = new Message(topic, tags, keys, msg.getBytes());
        return buildGoodsTransactionProducer.sendMessageInTransaction(message, null);
    }

}
